import api from '@/api'
import { createStore } from 'vuex'

export default createStore({
  state: {
	  role_id: '',
	  env_id: '',
	  user_id: '',
	  user_name: '',
	  pathPermission: {},
	  tags: [],
	  cur_path: '',
	  projectInfo: '',
	  interfaces: [],
  },
  getters: {
	  inner_interfaces(state){
		  return state.interfaces.filter((item)=>{
			  return item.type === 1
		  })
	  },
	  out_interfaces(state){
		  return state.interfaces.filter((item) =>{
			  return item.type === 2
		  })
	  }
	  
  },
  mutations: {
	  setPathPermission(state, path_permission){
		  state.pathPermission = path_permission
	  },
	  saveRoleId(state, id){
		  state.role_id = id
	  },
	  saveUserId(state, id){
		  state.user_id = id
	  },
	  saveCurPath(state, path){
	  	  state.cur_path = path
	  },
	  saveEnvId(state, id){
		  state.env_id = id
	  },
	  saveUserName(state, name){
		  state.user_name = name
	  },
	  saveInterface(state, value){
		  state.interfaces = value
	  },
	  saveProjectInfo(state, item){
		  state.projectInfo = {...item}
	  },
	  addTags(state, obj_item){
		  const obj = state.tags.find((item)=>{
			  return item.path === obj_item.path
		  })
		  if(!obj){
			 state.tags.push(obj_item)
		  }
		  console.log(state.tags, 'tags')
	  },
	  delTags(state, item){
		  const index = state.tags.findIndex((to)=>{
			  return to.path === item.path
		  })
		  state.tags.splice(index, 1)
	  },
	  clearTags(state){
		  const len= state.tags.length - 1
		  for (var index=len; index>=0; index--){
				state.tags.splice(index, 1)
		  }
	  }
  },
  actions: {
	  async getInterfaces(content, params){
		  const response = await api.getInterfaces(params)
		  if (response.status === 200){
			  return response.data
		  }
	  },
	  async getRolePermission(content, id){
	  		  const response = await api.getRolePermission(id)
	  		  if (response.status === 200){
	  			  return response.data
	  		  }
	  },
  },
  modules: {
  }
})
